package com.example.demo.ws;

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.MessageDigest;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Classname 前端轮询
 * @Description file:///Users/yuanby/yby/gitee/前端/ajaxDemo.html
 * @Date 2024/7/4 17:48
 * @Created by yby
 */
@RestController
@RequestMapping("/yby")
public class WebPollController {

    private static final Logger logger = LoggerFactory.getLogger(WebPollController.class);

    private final ConcurrentHashMap<Long, String> map = new ConcurrentHashMap<>();
    private final AtomicLong atomicLong = new AtomicLong(0);
    private final ConcurrentHashMap<Long, Long> timeoutMap = new ConcurrentHashMap<>();
//    @Autowired
//    private WsHandler wsHandler;

    @GetMapping("/hashHandler")
    public Long hashHandler(@RequestParam(name = "hash") String hash) {
        logger.info("进入hashHandler方法：,hash:{}", hash);
        // TODO 业务处理

        long id = atomicLong.incrementAndGet();
        map.put(id, hash);
        timeoutMap.put(id, System.currentTimeMillis() + 10000);

        return id;
    }

    @GetMapping("/hashHandlerResult")
    public Result<String> hashHandlerResult(@RequestParam(name = "id") Long id) {
        logger.info("进入hashHandler方法：,id:{}", id);
        // TODO 业务处理
        if (System.currentTimeMillis() > timeoutMap.get(id)) {
            return Result.create(0, "成功", "输入：" + map.get(id) + "，输出：" + sha256(map.get(id)));
        } else {
            return Result.create(1, "任务进行中", null);
        }


        //wsHandler.sendMessageByUserId(userId,"sdfsf");
    }

    @Data
    public static class Result<T> {
        private Integer code;
        private String message;
        private T data;

        public static <T> Result<T> create(Integer code, String message, T data) {
            Result<T> result = new Result<>();
            result.code = code;
            result.message = message;
            result.data = data;
            return result;
        }
    }

    public static String sha256(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(input.getBytes("UTF-8"));
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String myString = "Hello, World!";
        String hash = sha256(myString);
        System.out.println(hash);
    }

}
